<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="区域">
              <j-depart-select v-model="queryParam.EXTRA_DATA_orgCode"></j-depart-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="姓名">
              <a-input placeholder="请输入姓名或首字母" v-model="queryParam.firstLetter"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="身份证">
                <a-input placeholder="请输入身份证" @blur="idcardBlur" v-model="queryParam.idcard"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input placeholder="请输入手机号" v-model="queryParam.mobile"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'vcaResident:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button v-has="'vcaResident:exportXls'"  type="primary" icon="download" @click="handleExportXls('vca_resident')">导出</a-button>
      <a-upload v-has="'vcaResident:importExcel'"  name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-upload  name="file" :customRequest="customReq" :showUploadList="false"  :multiple="false" :headers="tokenHeader" :action="distributeExcelUrl" @change="distributeChange">
        <a-button type="primary" icon="import">任务分解</a-button>
      </a-upload>
<!--      <JFileUpload
        ref="myUpload"
        :action="distributeExcelUrl"
        name="files"
      ><a-button type="primary" icon="import">任务分配</a-button>
      </JFileUpload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-has="'vcaResident:deleteBatch'"  v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="idcard" slot-scope="text,record">
          <div class="idcard"> {{text}}
            <div class="badge">
              <span v-if="record.poor!=0" style="background-color: red;padding: 0 3px 0 3px;">脱贫</span>
              <span v-if="record.death==1" style="background-color: red">死</span>
              <span v-if="record.partyMember==1" style="background-color: #ffc800;color: #fb0000;">党</span>
            </div>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleFamilyDetail(record)">家庭成员</a>
          <a-divider type="vertical" />
          <a-dropdown v-has="'vcaResident:more'">
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleRemark(record)">备注</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleEdit(record)" v-has="'vcaResident:edit'">编辑</a>
              </a-menu-item>
              <a-menu-item v-has="'vcaResident:delete'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <vca-resident-modal ref="modalForm" @ok="modalFormOk"></vca-resident-modal>
    <vca-remark-list ref="remarkModalForm" type="resident"></vca-remark-list>
    <vca-family-detail ref="familyModalForm"></vca-family-detail>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import VcaResidentModal from './modules/VcaResidentModal'
  import JDepartSelect from '@/components/jeecg/JDepartSelect'
  import JUserSelect from '@/components/jeecg/JUserSelect'
  import VcaRemarkList from './VcaRemarkList'
  import VcaFamilyDetail from "./modules/VcaFamilyDetail";
  import myRequest from '@/utils/myRequest'

  export default {
    name: 'VcaResidentList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {VcaFamilyDetail, VcaRemarkList, VcaResidentModal,JDepartSelect,JUserSelect},
    data () {
      return {
        description: 'vca_resident管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'家庭编号',
            align:"center",
            dataIndex: 'familyCode'
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'residentName'
          },
          {
            title:'身份证',
            align:"center",
            dataIndex: 'idcard',
            scopedSlots: { customRender: 'idcard' }
          },
          {
            title:'手机号',
            align:"center",
            dataIndex: 'mobile'
          },
          {
            title:'关系',
            align:"center",
            dataIndex: 'familyRelation'
          },
          {
            title:'年龄',
            align:"center",
            dataIndex: 'year'
          },
          {
            title:'性别',
            align:"center",
            dataIndex: 'sex',
            customRender:function (sex) {
              return sex!=null?(sex==1?'男':'女'):'';
            }
          },
          {
            title:'户籍地址',
            align:"center",
            dataIndex: 'hostAddress'
          },
          {
            title:'创建者',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/villageCadreAssist/vcaResident/list",
          delete: "/villageCadreAssist/vcaResident/delete",
          deleteBatch: "/villageCadreAssist/vcaResident/deleteBatch",
          exportXlsUrl: "/villageCadreAssist/vcaResident/exportXls",
          importExcelUrl: "villageCadreAssist/vcaResident/importExcel",
          distributeExcelUrl: "villageCadreAssist/vcaResident/distribute",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      distributeExcelUrl:function(){
        return `${window._CONFIG['domianURL']}/${this.url.distributeExcelUrl}`;
      }
    },
    methods: {
      customReq(options){
        return myRequest(options);
      },
      distributeChange(info){
        if (info.file.status === 'done') {
          console.log("done",info);
          let blob = new Blob([info.file.response], {type: `application/octet-stream`});
          let a = document.createElement('a');
          a.href = window.URL.createObjectURL(blob);
          a.download = info.file.name;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          window.URL.revokeObjectURL(a.href);
        }else{
          console.log("info",info);
          this.$message.error("文件上传失败！");
        }

      },
      idcardBlur(){
        let idcard = this.queryParam.idcard;
        if(idcard && idcard.indexOf(" ")!=-1){
          idcard = idcard.trim().replaceAll(" ",",");
          this.queryParam.idcard = ","+idcard+",";
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'familyCode',text:'家庭编号',dictCode:''})
        fieldList.push({type:'string',value:'residentName',text:'姓名',dictCode:''})
        fieldList.push({type:'string',value:'idcard',text:'身份证',dictCode:''})
        fieldList.push({type:'string',value:'mobile',text:'手机号',dictCode:''})
        fieldList.push({type:'string',value:'familyRelation',text:'关系',dictCode:''})
        fieldList.push({type:'string',value:'birthday',text:'年龄',dictCode:''})//TODO
        fieldList.push({type:'int',value:'sex',text:'性别',dictCode:'sex'})
        fieldList.push({type:'int',value:'death',text:'是否死亡',dictCode:'yn'})
        fieldList.push({type:'int',value:'partyMember',text:'是否党员',dictCode:'yn'})
        fieldList.push({type:'int',value:'poor',text:'是否脱贫',dictCode:'yn'})
        this.superFieldList = fieldList
      },
      handleRemark(record){
          this.$refs.remarkModalForm.view(record);
          this.$refs.remarkModalForm.title = "备注";
      },
      handleFamilyDetail(record){
          this.$refs.familyModalForm.edit(record);
          this.$refs.familyModalForm.title = "家庭成员";
      },
      handleDeath(checked,event,record){
        console.log("handleDeath",checked);
        console.log("handleDeath record",record);
        if(checked){
          this.$refs.deathModal.add(record);
        }else{
          record.death = 0;
        }
      }
    }
  }
</script>
<style scoped >
  @import '~@assets/less/common.less';
  .ant-switch-checked {
    background-color: #f5222d;
  }
  .idcard{
    position: relative;
  }
  .idcard .badge {
    position: absolute;
    top: -15px;
    right: 0px;
  }
  .idcard .badge span{
    color: #fff;
    display: inline-block;
    border-radius: 9px;
    min-width: 18px;
    height: 18px;
    font-size: 10px;
  }
</style>